package com.we823.project.controller.manage;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.we823.project.controller.AbstractBaseController;
import com.we823.project.core.web.WebConstants;
import com.we823.project.model.StationModel;
import com.we823.project.model.UserModel;
import com.we823.project.model.UserStationModel;
import com.we823.project.repository.service.StationService;
import com.we823.project.repository.service.UserService;
import com.we823.project.repository.service.UserStationService;
import com.we823.utils.web.RequestUtil;

@Controller
@RequestMapping(WebConstants.MANAGE_USER_STATION)
public class UserStationController extends AbstractBaseController<UserStationModel> {

	private String baseUrl = WebConstants.MANAGE_USER_STATION_TEMPLATE;
	
	@Autowired
	private UserService userService;
	@Autowired
	private StationService stationService;
	
	private UserStationService userStationService;
	
	@Autowired
	public UserStationController(UserStationService userStationService) {
		super(userStationService);
		this.userStationService = userStationService;
		super.baseUrl = baseUrl;
		setResourceIdentity("system:user_station");
	}

	@Override
	public Page<UserStationModel> getData(PageRequest pageRequest,
			HttpServletRequest request, Model model, boolean pageable) {
		Page<UserStationModel> uPage = null;
		if(pageable) {
			uPage = userStationService.findAll(pageRequest);
		}else {
			uPage = new PageImpl<>(userStationService.findAll());
		}
		fillInfo(uPage);
		return uPage;
	}

	@Override
	public String preAdd(HttpServletRequest request, Model model) {
		List<UserModel> userModels = userService.findAll();
		List<StationModel> stationModels = stationService.findAll();
		model.addAttribute("users",userModels);
		model.addAttribute("stations",stationModels);
		
		return super.preAdd(request, model);
	}
	
	@Override
	public String preEdit(HttpServletRequest request, Model model) {
		List<UserModel> userModels = userService.findAll();
		List<StationModel> stationModels = stationService.findAll();
		model.addAttribute("users",userModels);
		model.addAttribute("stations",stationModels);
		return super.preEdit(request, model);
	}
	private void fillInfo(Page<UserStationModel> uPage) {
		if(uPage!=null) {
			Map<Integer, UserModel> userMap = userService.map();
			Map<Integer, StationModel> stationMap = stationService.map();
			
			for (UserStationModel userStationModel : uPage) {
				if(userMap.containsKey(userStationModel.getUserId())) {
					userStationModel.setUserName(userMap.get(userStationModel.getUserId()).getUsername());
				}else {
					userStationModel.setUserName("未知");
				}
				
				
				if(stationMap.containsKey(userStationModel.getStationId())) {
					userStationModel.setStationName(stationMap.get(userStationModel.getStationId()).getName());
				}else {
					userStationModel.setStationName("未知");
				}
			}
		}
		
	}

	@RequestMapping("findSame")
	@ResponseBody
	public boolean findSame(HttpServletRequest request) {
		Integer stationId = RequestUtil.getValueByParam(request, "stationId", 0);
		Integer userId = RequestUtil.getValueByParam(request, "userId", 0);
		if(userId>0 && stationId>0) {
			UserStationModel userStationModel = userStationService.findByUserIdAndStationId(userId,stationId);
			if (userStationModel!=null) {
				return false;
			}
		}
		return true;
	}
}
